<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>欢迎注册</title>
    <link href="css/register.css" rel="stylesheet">
</head>
<body>

<div class="form-div">
    <div class="reg-content">
        <h1>欢迎注册</h1>
        <span>已有帐号？</span> <a href="login.html">登录</a>
    </div>
    <form id="reg-form" action="/servlet-loginCase2/register" method="post">

        <table>

            <tr>
                <td>用户名</td>
                <td class="inputs">
                    <input name="username" type="text" id="username" onblur="onCheckUserName()">
                    <br>
                    <span id="username_err" class="err_msg" style="display: none">用户名不太受欢迎</span>
                </td>

            </tr>

            <tr>
                <td>密码</td>
                <td class="inputs">
                    <input name="password" type="password" id="password">
                    <br>
                    <span id="password_err" class="err_msg" style="display: none">密码格式有误</span>
                </td>
            </tr>

            <tr>
                <td>验证码</td>
                <td class="inputs">
                    <input name="checkCode" type="text" id="checkCode">
                    <img id="checkCodeImg" src="/servlet-loginCase2/checkCode">
                    <a href="#" id="changeImg" >看不清？</a>
                </td>
            </tr>

        </table>

        <div class="buttons">
            <input value="注 册" type="submit" id="reg_btn">
        </div>
        <br class="clear">
    </form>

</div>
<script type="text/javascript">

    document.getElementById("changeImg").onclick = function (){

        document.getElementById("checkCodeImg").src= "/servlet-loginCase2/checkCode?"+ new Date().getMilliseconds()

    }

    /**
     * 校验用户名
     */
    function  onCheckUserName()
    {

        //1.获取输入用户名

        var username =document.getElementById("username").value;

        //2.发送Ajax请求校验用户名是否存在
        var xmlhttp = new XMLHttpRequest();

        xmlhttp.open("GET","/servlet-loginCase2/userCheckNameServlet?userName="+username);
        xmlhttp.send();

        xmlhttp.onreadystatechange = function ()
        {
            if(xmlhttp.status = 200 && xmlhttp.readyState == 4)
            {
                var result = xmlhttp.responseText;
                if(result == "false")
                {
                    //获取到隐藏提示信息标签
                    document.getElementById("username_err").style.display = "block";
                }else
                {
                    document.getElementById("username_err").style.display = "none";

                }
            }
        }
    }

</script>
</body>
</html>